package com.zhuanzhuan.hero.user.service.impl;

import com.google.common.reflect.TypeToken;
import com.zhuanzhuan.hero.user.consts.ApolloConsts;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import com.zhuanzhuan.hero.user.pojo.bo.*;
import com.zhuanzhuan.hero.user.service.IApolloService;
import com.zhuanzhuan.hero.user.util.Assert;
import com.zhuanzhuan.hero.user.vo.OpenMemberGuideVO;
import com.zhuanzhuan.hero.user.vo.setup.SettingItemVO;
import com.zhuanzhuan.youpin.offline.apollo.ApolloOper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.*;

/**
 * @ClassName ApolloServiceImpl
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/18 11:26
 */
@Service
@Slf4j
public class ApolloServiceImpl implements IApolloService {

    private final ApolloOper apolloOper = new ApolloOper(ApolloConsts.NAMESPACE);

    @Override
    public UserInfoDefaultBO queryDefauleProperties() {
        return Optional.ofNullable(apolloOper.get(ApolloConsts.DEFAULT_PROFILE_PHOTO_KEY, UserInfoDefaultBO.class))
            .orElseThrow(() -> new BusinessException(ApolloConsts.DEFAULT_PROFILE_PHOTO_KEY + "未配置"));
    }

    @Override
    public List<CateBO> queryDisplayCates() {
        return apolloOper.getList(ApolloConsts.CONTENT_CATEGORY_LABEL_CATES, CateBO.class);
    }

    @Override
    public DisplayIconBO queryDisplayIcon() {
        return apolloOper.get(ApolloConsts.DISPLAY_ICON, DisplayIconBO.class);
    }


    @Override
    public List<SettingItemVO> querySetupOptions(Integer sourceType) {
        Type type = new TypeToken<Map<Integer, List<SettingItemVO>>>(){}.getType();
        Map<Integer, List<SettingItemVO>> map = apolloOper.getMap(ApolloConsts.SETUP_LIST_OPTIONS, type);
        if (MapUtils.isEmpty(map)) {
            return new ArrayList<>();
        }

        return map.get(sourceType);
    }

    @Override
    public AppInitConfigBO queryAppInitConfig() {
        return Optional.ofNullable(apolloOper.get(ApolloConsts.APP_INIT_CONFIG, AppInitConfigBO.class))
            .orElseThrow(() -> new BusinessException("app_init_config未配置"));
    }

    @Override
    public String queryIosVirtualLoginVer() {
        return apolloOper.getString(ApolloConsts.IOS_VIRTUAL_LOGIN_VER);
    }

    @Override
    public List<AppInitConfAndroidBO> queryIosVirtualLoginVerAndroid() {
        return apolloOper.getList(ApolloConsts.ANDROID_VIRTUAL_LOGIN_VER, AppInitConfAndroidBO.class);
    }

    @Override
    public PageUrlBO queryPageUrls() {
        PageUrlBO pageUrlBO = apolloOper.get(ApolloConsts.PAGE_URL, PageUrlBO.class);
        Assert.notNull(pageUrlBO, ExceptionMsgEnum.DATA_NOT_EXIST);
        return pageUrlBO;
    }

    @Override
    public AppUpgradesVersionBO queryAppUpgradesVersion() {
        return apolloOper.get(ApolloConsts.APP_UPGRADES_VERSION, AppUpgradesVersionBO.class);
    }

    @Override
    public Boolean appUpgradesSwitch() {
        return apolloOper.get(ApolloConsts.APP_UPGRADES_VERSION_SWITCH, Boolean.class);
    }

    @Override
    public SwitchConfigBO queryLogicSwitchConfig() {
        return Optional.ofNullable(apolloOper.get(ApolloConsts.LOGIC_CHECK_SWITCH, SwitchConfigBO.class))
            .orElse(new SwitchConfigBO());
    }

    @Override
    public OpenMemberGuideVO queryOpenMemberGuide() {
        return Optional.ofNullable(apolloOper.get(ApolloConsts.MEMBER_GUIDE_CONTENT, OpenMemberGuideVO.class))
            .orElseThrow(() -> new BusinessException(ExceptionMsgEnum.DATA_NOT_EXIST));
    }

    @Override
    public TipsBO queryTips() {
        return Optional.ofNullable(apolloOper.get(ApolloConsts.TIPS_CONTENT, TipsBO.class))
            .orElseThrow(() -> new BusinessException(ExceptionMsgEnum.DATA_NOT_EXIST));
    }
}
